﻿/*
 * Copyright (C) 2013 David Neal Parsons
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace TripleAMapGenerator
{
    public partial class FormGenerateMap : Form
    {
        private Random rand;
        private int randomSeed = (int)(DateTime.Now.Ticks & 0x0000FFFF);
        private MapGenerator mg = new MapGenerator();
        private string errorMsg;

        public FormGenerateMap()
        {
            InitializeComponent();

            this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
        }

        private void FormGenerateMap_Load(object sender, EventArgs e)
        {
            // Basic
            string userFolder = System.IO.Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
            this.textBoxXMLFolder.Text = userFolder + @"\triplea\maps\Random_Campaign\games";

            string fileStamp = DateTime.Now.Year.ToString("D4") + DateTime.Now.Month.ToString("D2") + DateTime.Now.Day.ToString("D2")
                + DateTime.Now.Hour.ToString("D2") + DateTime.Now.Minute.ToString("D2") + DateTime.Now.Second.ToString("D2");
            this.textBoxMapName.Text = "RC_R" + fileStamp + "_FFA";

            rand = new Random(randomSeed);
            randomSeed = rand.Next();
            rand = new Random(randomSeed);
            this.textBoxRandomSeed.Text = randomSeed.ToString();
            rand.Next();

            this.textBoxDiceSides.Text = "6";

            // Pleyers
            this.textBoxNumberPlayers.Text = "6";
            this.textBoxCapitalSize.Text = "7";
            this.textBoxDistance.Text = "5";
            this.textBoxResourcesGiven.Text = "20";
            this.textBoxBoost.Text = "4";
            this.textBoxBid.Text = "30";
            this.checkBoxOwnCapitals.Checked = false;
            this.checkBoxOwnVC.Checked = false;
            this.checkBoxCTF.Checked = false;

            // Territories
            this.textBoxWidth.Text = "20";
            this.textBoxHeight.Text = "20";
            this.textBoxExtras.Text = "20";
            this.textBoxVC.Text = "1";
            this.textBoxPercentWater.Text = "20";
            this.textBoxPercentImpass.Text = "2";
            this.textBoxPercentForest.Text = "15";
            this.textBoxPercentMtn.Text = "5";
            this.textBoxPercentDesert.Text = "0";
            this.textBoxPercentHill.Text = "0";
            this.textBoxPercentMarsh.Text = "0";
            this.textBoxPercentCity.Text = "1";

            // Units
            this.checkBoxIncludeAdditional.Checked = false;

            // Tech
            this.checkBoxDefaultTech.Checked = false;
            this.textBoxPercentTech.Text = "15";
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBoxXMLFolder.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            rand = new Random(randomSeed);
            randomSeed = rand.Next();
            rand = new Random(randomSeed);
            this.textBoxRandomSeed.Text = randomSeed.ToString();
            rand.Next();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioButtonFFA_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonFFA.Checked)
            {
                this.textBoxNumberTeams.Text = "0";
                this.textBoxNumberTeams.Enabled = false;
                if (this.textBoxMapName.Text.Contains("_Teams"))
                {
                    this.textBoxMapName.Text = this.textBoxMapName.Text.Replace("_Teams", "_FFA");
                }
            }
            else
            {
                this.textBoxNumberTeams.Text = "2";
                this.textBoxNumberTeams.Enabled = true;
                if (this.textBoxMapName.Text.Contains("_FFA"))
                {
                    this.textBoxMapName.Text = this.textBoxMapName.Text.Replace("_FFA", "_Teams");
                }
            }
        }

        private void checkBoxOwnVC_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxOwnVC.Checked)
            {
                this.textBoxPercentOwnVC.Text = "80";
                this.textBoxPercentOwnVC.Enabled = true;
            }
            else
            {
                this.textBoxPercentOwnVC.Text = "0";
                this.textBoxPercentOwnVC.Enabled = false;
            }
        }

        private void buttonGenerateMap_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                mg = new MapGenerator();
                GetUI();
                mg.Map.MapInitialize();

                string msg = "";
                if (mg.Map.ErrorList.Count > 0)
                {
                    foreach (string error in mg.Map.ErrorList)
                    {
                        msg = msg + error + "\r\n";
                    }
                    msg = msg + "\r\nTry one of the following:\r\n";
                    msg = msg + " - A new seed.\r\n";
                    msg = msg + " - Increasing the map size.\r\n";
                    msg = msg + " - Reducing one of the other settings.\r\n";
                    MessageBox.Show(msg);
                }
                else
                {
                    mg.CreateLines();

                    string outFile = mg.Map.MapFolder + "\\" + mg.Map.MapName + ".xml";

                    Cursor.Current = Cursors.WaitCursor;
                    Application.DoEvents();
                    try
                    {
                        using (StreamWriter xmlFile = new StreamWriter(outFile, false))
                        {
                            foreach (string xmlLine in mg.XmlLines)
                            {
                                xmlFile.WriteLine(xmlLine);
                            }
                        }
                        MessageBox.Show("Map generation complete.");
                    }
                    finally
                    {
                        Cursor.Current = Cursors.Default;
                        Application.DoEvents();
                    }
                }
            }
            else
            {
                MessageBox.Show("Some controls have invalid values. Hover the mouse over the red exclamation to see details.");
            }
        }

        private void buttonSurpriseMe_Click(object sender, EventArgs e)
        {
            RandomizeUI();
            buttonGenerateMap_Click(sender, e);
        }

        private void GetUI()
        {
            // Basic
            mg.Map.Rand = rand;
            mg.Map.MapFolder = this.textBoxXMLFolder.Text;
            mg.Map.MapName = this.textBoxMapName.Text;
            mg.Map.RandomSeed = Convert.ToInt32(this.textBoxRandomSeed.Text);
            mg.Map.DiceSides = Convert.ToInt32(this.textBoxDiceSides.Text);

            if (this.radioButtonWW2V2.Checked)
            {
                mg.Map.WW2V2 = true;
            }
            else
            {
                mg.Map.WW2V2 = false;
            }

            // Players
            mg.Map.NumberOfPlayers = Convert.ToInt32(this.textBoxNumberPlayers.Text);

            if (this.radioButtonFFA.Checked)
            {
                mg.Map.FFA = true;
            }
            else
            {
                mg.Map.FFA = false;
            }

            if (String.IsNullOrEmpty(this.textBoxNumberTeams.Text))
            {
                mg.Map.NumberOfTeams = 0;
            }
            else
            {
                mg.Map.NumberOfTeams = Convert.ToInt32(this.textBoxNumberTeams.Text);
            }

            mg.Map.CapitalSize = Convert.ToInt32(this.textBoxCapitalSize.Text);
            mg.Map.CapitalDistance = Convert.ToInt32(this.textBoxDistance.Text);
            mg.Map.ResourcesGiven = Convert.ToInt32(this.textBoxResourcesGiven.Text);
            mg.Map.BoostPerPlayer = Convert.ToInt32(this.textBoxBoost.Text);
            mg.Map.Bid = Convert.ToInt32(this.textBoxBid.Text);
            mg.Map.OwnCapitals = this.checkBoxOwnCapitals.Checked;
            mg.Map.OwnPercentVC = this.checkBoxOwnVC.Checked;

            if (String.IsNullOrEmpty(this.textBoxPercentOwnVC.Text))
            {
                mg.Map.OwnPercentVCAmount = 0;
            }
            else
            {
                mg.Map.OwnPercentVCAmount = Convert.ToInt32(this.textBoxPercentOwnVC.Text);
            }

            mg.Map.CTF = this.checkBoxCTF.Checked;
            mg.Map.UseTriggers = this.checkBoxCTF.Checked | this.checkBoxOwnCapitals.Checked | this.checkBoxOwnVC.Checked;

            //Territories
            mg.Map.NumberOfColumns = Convert.ToInt32(this.textBoxWidth.Text);
            mg.Map.NumberOfRows = Convert.ToInt32(this.textBoxHeight.Text);
            mg.Map.PercentNeutralExtras = Convert.ToInt32(this.textBoxExtras.Text);
            mg.Map.PercentVC = Convert.ToInt32(this.textBoxVC.Text);
            mg.Map.PercentWater = Convert.ToInt32(this.textBoxPercentWater.Text);
            mg.Map.PercentImpassable = Convert.ToInt32(this.textBoxPercentImpass.Text);
            mg.Map.PercentForest = Convert.ToInt32(this.textBoxPercentForest.Text);
            mg.Map.PercentMtn = Convert.ToInt32(this.textBoxPercentMtn.Text);
            mg.Map.PercentDesert = Convert.ToInt32(this.textBoxPercentDesert.Text);
            mg.Map.PercentHill = Convert.ToInt32(this.textBoxPercentHill.Text);
            mg.Map.PercentMarsh = Convert.ToInt32(this.textBoxPercentMarsh.Text);
            mg.Map.PercentCity = Convert.ToInt32(this.textBoxPercentCity.Text);

            // Units
            mg.Map.UseAdditional = this.checkBoxIncludeAdditional.Checked;

            // Tech
            mg.Map.UseTech = this.checkBoxDefaultTech.Checked;
            mg.Map.PercentTech = Convert.ToInt32(this.textBoxPercentTech.Text);
            mg.Map.InitialJet = this.checkBoxJet.Checked;
            mg.Map.InitialSub = this.checkBoxSub.Checked;
            mg.Map.InitialAir = this.checkBoxAir.Checked;
            mg.Map.InitialDestroyer = this.checkBoxDestroyer.Checked;
            mg.Map.InitialBomber = this.checkBoxBomber.Checked;
            mg.Map.InitialIT = this.checkBoxIT.Checked;
        }

        private void RandomizeUI()
        {
            // Basic
            rand = new Random(randomSeed);
            randomSeed = rand.Next();
            rand = new Random(randomSeed);
            this.textBoxRandomSeed.Text = randomSeed.ToString();
            rand.Next();

            // Players
            this.textBoxNumberPlayers.Text = rand.Next(2, 9).ToString();
            this.radioButtonFFA.Checked = rand.Next(0, 3) == 0 ? false : true;
            string ext = this.radioButtonFFA.Checked ? "_FFA" : "_Teams";
            string fileStamp = DateTime.Now.Year.ToString("D4") + DateTime.Now.Month.ToString("D2") + DateTime.Now.Day.ToString("D2")
                + DateTime.Now.Hour.ToString("D2") + DateTime.Now.Minute.ToString("D2") + DateTime.Now.Second.ToString("D2");
            this.textBoxMapName.Text = "RC_R" + fileStamp + ext;

            if (this.radioButtonFFA.Checked)
            {
                this.radioButtonTeams.Checked = false;
            }
            else
            {
                this.radioButtonTeams.Checked = true;
            }

            if (this.radioButtonTeams.Checked)
            {
                this.textBoxNumberTeams.Text = rand.Next(2, 5).ToString();
            }
            else
            {
                this.textBoxNumberTeams.Text = "0";
            }

            this.textBoxCapitalSize.Text = rand.Next(1, 8).ToString();
            this.textBoxDistance.Text = rand.Next(3, 11).ToString();
            this.textBoxResourcesGiven.Text = rand.Next(0, 51).ToString();
            this.textBoxBoost.Text = rand.Next(0, 11).ToString();
            this.textBoxBid.Text = rand.Next(0, 51).ToString();

            this.checkBoxOwnCapitals.Checked = false;
            this.checkBoxOwnVC.Checked = false;
            this.checkBoxCTF.Checked = false;
            
            int vcType = rand.Next(0, 101);
            if (vcType < 20)
            {
                this.checkBoxOwnCapitals.Checked = true;
            }
            else if (vcType < 25)
            {
                this.checkBoxOwnVC.Checked = true;
            }
            else if (vcType < 30)
            {
                this.checkBoxCTF.Checked = true;
            }

            // Territories
            this.textBoxWidth.Text = rand.Next(5, 21).ToString();
            this.textBoxHeight.Text = rand.Next(5, 21).ToString();
            this.textBoxExtras.Text = rand.Next(0, 51).ToString();
            this.textBoxVC.Text = rand.Next(0, 11).ToString();
            this.textBoxPercentWater.Text = rand.Next(0, 51).ToString();
            this.textBoxPercentImpass.Text = rand.Next(0, 21).ToString();
            this.textBoxPercentForest.Text = rand.Next(0, 51).ToString();
            this.textBoxPercentMtn.Text = rand.Next(0, 11).ToString();
            this.textBoxPercentDesert.Text = rand.Next(0, 11).ToString();
            this.textBoxPercentHill.Text = rand.Next(0, 11).ToString();
            this.textBoxPercentMarsh.Text = rand.Next(0, 11).ToString();
            this.textBoxPercentCity.Text = rand.Next(0, 11).ToString();

            // Units
            this.checkBoxIncludeAdditional.Checked = rand.Next(0, 5) == 0 ? true : false;

            // Tech
            this.checkBoxDefaultTech.Checked = rand.Next(0, 5) == 0 ? true : false;
        }

        private bool IsValidFolder(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                errorMsg = "Browse to and select the Random_Campaign\\games folder under your TripleA maps folder.";
                return false;
            }

            if (!System.IO.Directory.Exists(text))
            {
                errorMsg = "Browse to and select the Random_Campaign\\games folder under your TripleA maps folder.";
                return false;
            }

            errorMsg = "";
            return true;
        }

        private bool IsValidText(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                errorMsg = "Required.";
                return false;
            }

            errorMsg = "";
            return true;
        }

        private bool IsValidNumber(string text, int min, int max)
        {
            int number = -1;
            if (string.IsNullOrEmpty(text))
            {
                errorMsg = "Must be an integer";
                errorMsg = errorMsg + " between " + min.ToString() + " and " + max.ToString() + ".";
                return false;
            }
            else if (int.TryParse(text, out number))
            {
                if (!(number >= min && number <= max))
                {
                    errorMsg = "Must be an integer";
                    errorMsg = errorMsg + " between " + min.ToString() + " and " + max.ToString() + ".";
                    return false;
                }
            }

            errorMsg = "";
            return true;
        }

        private void textBoxXMLFolder_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidFolder(((TextBox)sender).Text))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxXMLFolder_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxWidth_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 5, 20))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxWidth_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxHeight_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 5, 20))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxHeight_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxMapName_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidText(((TextBox)sender).Text))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxMapName_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxRandomSeed_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, int.MaxValue))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxRandomSeed_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxNumberPlayers_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 2, 8))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxNumberPlayers_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxNumberTeams_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 2, 4))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxNumberTeams_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentWater_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 90))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentWater_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentImpass_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentImpass_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxCapitalSize_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 1, 7))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxCapitalSize_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxResourcesGiven_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 99))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxResourcesGiven_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxBoost_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 25))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxBoost_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxExtras_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 99))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxExtras_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxBid_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxBid_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxDistance_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 2, 19))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxDistance_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxVC_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 10))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxVC_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentForest_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentForest_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentMtn_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentMtn_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentDesert_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentDesert_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentHill_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentHill_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentMarsh_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentMarsh_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentCity_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 50))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentCity_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxDiceSides_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 1, 200))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxDiceSides_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentTech_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 1, 99))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentTech_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }

        private void textBoxPercentOwnVC_Validating(object sender, CancelEventArgs e)
        {
            if (IsValidNumber(((TextBox)sender).Text, 0, 100))
            {
                e.Cancel = false;
            }
            else
            {
                this.errorProvider1.SetError((TextBox)sender, errorMsg);
                e.Cancel = true;
            }
        }

        private void textBoxPercentOwnVC_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError((TextBox)sender, string.Empty);
        }
    }
}
